
#ifndef __SUBDIVISIONGEOM_H__
#define __SUBDIVISIONGEOM_H__

#include "Geometry.h"
//#include "boost/Thread/Thread.hpp"

class SubdivisionGeom : public Geometry
{
public:

	// Constructor
	SubdivisionGeom(Entity *_parent);

	// Destructor
	~SubdivisionGeom();

	// Clears SubMeshes
	void Clear();

	// Subdivision
	void IncreaseSubLevel();
	void DecreaseSubLevel();

	// Adds a vertex to the mesh
	int AddVertex(Vertex &v);
	int AddVertex(Real3 position, Real3 normal = Real3(0.0f, 1.0f, 0.0f), Real2 tex = Real2(0.0f, 0.0f));
	int AddUniqueVertex(Real3 position, Real3 normal = Real3(0.0f, 1.0f, 0.0f), Real2 tex = Real2(0.0f, 0.0f));

	// Adds a triangle to the mesh
	int AddTriangle(Triangle &t);
	int AddTriangle(uint a, uint b, uint c);

	// Adds a quad to the mesh
	int AddQuad(Quad &q);
	int AddQuad(uint a, uint b, uint c, uint d);

	// Draws a grid
	void DrawGrid(uint width, uint height, float size = 1.0f);
	void DrawHeightMap(vector< vector<uchar> > &hMap, float scale = 1.0f);

	// Draws a Box, and sets tex coords
	void DrawBox(float sideLength);

	// Rotate
	void Rotate(const D3DXMATRIX *rotation);

private:

	// Subdivision Meshes
	vector<Mesh> subMeshes;

	// Using subdividing data
	uchar subLevel;

	// For threading
	//boost::mutex mSubdividing;
	//boost::mutex mUpdating;

	// Renders the correct subMesh
	void _Render(uchar renderFlag);

	// Subdivides using Catmull-Clark Method (Caution! Keep num under 5)
	// Vertices should have correct normals before hand, if not call
	// CalculateVertexNormals() first, returns false if takes too long
	bool CreateSubdivision(ushort level);

	// Called by create Sub when it ends prematurely
	void CleanPartialSubMesh();

	// Generates subMeshes from a max detail height map
	void GenerateHeightMapSubMeshes(float sideLength);

	// Updates the Normals array
	void AddNormals();
};

#endif // __SUBDIVISIONGEOM_H__